package cn.com.lyj6851.modules.order.service.impl;

import cn.com.lyj6851.common.service.impl.BaseServiceImpl;
import cn.com.lyj6851.common.utils.QueryHelpPlus;
import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.modules.order.domain.YdyExpress;
import cn.com.lyj6851.modules.order.service.YdyExpressService;
import cn.com.lyj6851.modules.order.service.dto.YdyExpressDto;
import cn.com.lyj6851.modules.order.service.dto.YdyExpressQueryCriteria;
import cn.com.lyj6851.modules.order.service.mapper.ExpressMapper;
import cn.com.lyj6851.utils.FileUtil;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YdyExpressServiceImpl extends BaseServiceImpl<ExpressMapper, YdyExpress> implements YdyExpressService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YdyExpressQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YdyExpress> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YdyExpressDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<YdyExpress> queryAll(YdyExpressQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YdyExpress.class, criteria));
    }


    @Override
    public void download(List<YdyExpressDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YdyExpressDto yxExpress : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("快递公司简称", yxExpress.getCode());
            map.put("快递公司全称", yxExpress.getName());
            map.put("排序", yxExpress.getSort());
            map.put("是否显示", yxExpress.getIsShow());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}